package cn.tuling.org.controller;

import cn.tuling.basic.util.AjaxResult;
import cn.tuling.basic.util.PageList;
import cn.tuling.org.domain.User;
import cn.tuling.org.query.UserQuery;
import cn.tuling.org.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 宋俊林
 * @create 2022/10/1 19:54
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户的API",description="用户相关的CRUD功能")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @author 宋俊林
     * @date 2022/10/1 22:13
     * 查询所有用户信息
     */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<User> findAll(){
        return userService.findAll();
    }

    /**
     * @author 宋俊林
     * @date 2022/10/1 22:13
     * 根据id查询
     */
    @GetMapping("/{id}")
    public User findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return userService.findById(id);
    }

    /**
     * @author 宋俊林
     * @date 2022/10/2 17:46
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        userService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * @author 宋俊林
     * @date 2022/10/1 22:15
     * 添加和修改用户
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        if (null == user.getId()){
            userService.add(user);
        } else {
            userService.update(user);
        }
        return AjaxResult.me().success();
    }

    /**
     * @author 宋俊林
     * @date 2022/10/2 16:18
     * 分页查询
     */
    @PostMapping
    public PageList<User> queryByPage(@RequestBody UserQuery userQuery){
        return userService.queryByPage(userQuery);
    }


}
